﻿using System;
using System.Collections.Generic;
using System.Text;
using Npgsql;
using System.Collections;
using System.IO;
using System.Data;

namespace app_common
{
    public static class APP_CONNECTION
    {
        public static Hashtable hashDbInfo = new Hashtable();

        public static NpgsqlConnection Conn;

        public static Hashtable Close() {
            Hashtable result = new Hashtable();
            try {
                if (APP_CONNECTION.Conn.State == ConnectionState.Open)
                APP_CONNECTION.Conn.Close();
                result.Add(STATUS_MESSAGE.MESSAGE, STATUS_MESSAGE.SUCCESS);
                result.Add(STATUS_MESSAGE.VALUE, "Connection closed");
            }catch(Exception ex){
                result.Add(STATUS_MESSAGE.MESSAGE, STATUS_MESSAGE.ERROR);
                result.Add(STATUS_MESSAGE.VALUE, ex.Message);
            }
            return result;
        }

        public static Hashtable Open()
        {
            Hashtable result = new Hashtable();
            try
            {
                if (APP_CONNECTION.Conn.State == ConnectionState.Closed)
                    APP_CONNECTION.Conn.Open();
                result.Add(STATUS_MESSAGE.MESSAGE, STATUS_MESSAGE.SUCCESS);
                result.Add(STATUS_MESSAGE.VALUE, "Connection opened");
            }
            catch (Exception ex)
            {
                result.Add(STATUS_MESSAGE.MESSAGE, STATUS_MESSAGE.ERROR);
                result.Add(STATUS_MESSAGE.VALUE, ex.Message);
            }
            return result;
        }

        public static Hashtable ConnectDatabase()
        {
            Hashtable result = new Hashtable();
            try {
                string conn_string = "Server=" + hashDbInfo["server"].ToString() + 
                                        ";Port=" + hashDbInfo["port"].ToString() + 
                                        ";Database=" + hashDbInfo["database"].ToString() + 
                                        ";User Id=" + hashDbInfo["user"].ToString() + 
                                        ";Password=" + hashDbInfo["password"].ToString();
                APP_CONNECTION.Conn = new NpgsqlConnection(conn_string);
                result = APP_CONNECTION.Open();
                if (result[STATUS_MESSAGE.MESSAGE].ToString() == STATUS_MESSAGE.SUCCESS)
                {
                    result = APP_CONNECTION.Close();
                    if (result[STATUS_MESSAGE.MESSAGE].ToString() == STATUS_MESSAGE.SUCCESS)
                    {
                        result[STATUS_MESSAGE.VALUE] = "Connection connected";
                    }
                    else
                    {
                        result[STATUS_MESSAGE.VALUE] = "Không thể kết nối đến cơ sở dữ liệu.\n\rChi tiết: " + result[STATUS_MESSAGE.VALUE].ToString();
                    }
                }
                else {
                    result[STATUS_MESSAGE.VALUE] = "Không thể kết nối đến cơ sở dữ liệu.\n\rChi tiết: " + result[STATUS_MESSAGE.VALUE].ToString();
                }

            }catch(Exception ex){
                result.Add(STATUS_MESSAGE.MESSAGE, STATUS_MESSAGE.ERROR);
                result.Add(STATUS_MESSAGE.VALUE, "Không thể kết nối đến cơ sở dữ liệu.\n\rChi tiết: " + ex.Message);
            }
            return result;        
        }

        public static Hashtable ReadDbConfig()
        {
            Hashtable result = new Hashtable();
            try {
                StreamReader sr = new StreamReader("database.conf");
                string text;
                while((text = sr.ReadLine()) != null){
                    if (text.IndexOf("[") >= 0) {
                        continue;
                    }
                    if (text.Trim() == string.Empty) {
                        continue;
                    }
                    string[] arr_text = text.Split(new Char[]{'='});
                    hashDbInfo.Add(arr_text[0].Trim().ToLower(), arr_text[1].Trim());
                }
                sr.Close();
                result.Add(STATUS_MESSAGE.MESSAGE, STATUS_MESSAGE.SUCCESS);
                result.Add(STATUS_MESSAGE.VALUE, "Read config successfully");
            }catch(Exception ex){
                result.Add(STATUS_MESSAGE.MESSAGE, STATUS_MESSAGE.ERROR);
                result.Add(STATUS_MESSAGE.VALUE, ex.Message);
            }
            return result;
        }
    }
}
